package pers.cnzdy.tutorial.Chapter7_Data.Section4_ContentProvider;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import pers.cnzdy.tutorial.R;

public class QuizResolverActivity extends AppCompatActivity {
    ContentResolver contentResolver;
    Button insert = null;
    Button query = null;
    ListView listView = null;
    EditText et_statement;
    EditText et_type;
    EditText et_answer;
    EditText et_difficulty;

    String newId;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_quiz_resolver);

        // 获取系统的ContentResolver对象
        contentResolver = getContentResolver();
        insert = findViewById(R.id.insert);
        query = findViewById(R.id.query);
        listView = findViewById(R.id.show);
        et_statement = findViewById(R.id.et_statement);
        et_type = findViewById(R.id.et_type);
        et_answer = findViewById(R.id.et_answer);
        et_difficulty= findViewById(R.id.et_difficulty);

        // 为insert按钮的单击事件绑定事件监听器
        insert.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View source)
            {
                // 获取用户输入
                String statement = et_statement.getText().toString();
                String type = et_type.getText().toString();
                String answer = et_answer.getText().toString();
                String difficulty = et_difficulty.getText().toString();

                // 插入测试题
                ContentValues values = new ContentValues();
                values.put(QuizSet.STATEMENT, statement);
                values.put(QuizSet.TYPE, type);
                values.put(QuizSet.ANSWER, answer);
                values.put(QuizSet.DIFFICULTY, difficulty);

                Uri uri = Uri.parse("content://pers.cnzdy.tutorial.quiz.provider/quiz");
                Uri newUri = contentResolver.insert(uri, values);
                newId = newUri.getPathSegments().get(1);

                // 显示提示信息
                Toast.makeText(QuizResolverActivity.this,
                        "添加题目成功！" + newId, Toast.LENGTH_SHORT).show();
            }
        });

        // 为search按钮的单击事件绑定事件监听器
        query.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View source)
            {
                Uri uri = Uri.parse("content://pers.cnzdy.tutorial.quiz.provider/quiz");

                // 执行查询
                Cursor cursor = contentResolver.query(uri, null,
                        null,null,null);

                List<Map<String, String>> list = converCursorToList(cursor);

                // SimpleAdapter适配List
                SimpleAdapter adapter = new SimpleAdapter(QuizResolverActivity.this, list,
                        R.layout.item_quiz, new String[] { QuizSet.ID, QuizSet.STATEMENT,
                                         QuizSet.TYPE, QuizSet.ANSWER, QuizSet.DIFFICULTY },
                        new int[] { R.id.id_show, R.id.statement_show, R.id.type_show,
                                    R.id.answer_show, R.id.difficulty_show});
                listView.setAdapter(adapter);
            }
        });
    }

    private ArrayList<Map<String, String>> converCursorToList(Cursor cursor)
    {
        ArrayList<Map<String, String>> result = new ArrayList<>();

        // 遍历Cursor结果集
        while (cursor.moveToNext())
        {
            // 将结果集中的数据存入ArrayList中
            Map<String, String> map = new HashMap<>();
            map.put(QuizSet.ID, cursor.getString(0));
            map.put(QuizSet.STATEMENT, cursor.getString(1));
            map.put(QuizSet.TYPE, cursor.getString(2));
            map.put(QuizSet.ANSWER, cursor.getString(3));
            map.put(QuizSet.DIFFICULTY, cursor.getString(4));
            result.add(map);
        }
        return result;
    }
}
